#! /usr/bin/env python2

# Author: Raymond Barrett
# Email: barretrj@tcd.ie

import pygame
import json
from random import randint

from pygame.locals import *

SCREENSIZE = [1024,768]
BOXSIZE = (300,500)
BUBBLESIZE = (300, 86)
AVATARSIZE = (60, 60)
POS = (SCREENSIZE[0]/2 - BOXSIZE[0]/2, SCREENSIZE[1]/2 - BOXSIZE[1]/2)

class Staff(object):
	"""docstring for Staff"""
	pygame.font.init()
	myfont = pygame.font.SysFont("arial", 18)

	bg1 = pygame.image.load('Images/wordlebubble.png')
	bg1 = pygame.transform.scale(bg1, BOXSIZE)
	# bg2 = pygame.image.load('Images/WordleWordBox2.gif')
	# bg2 = pygame.transform.scale(bg2, (BOXSIZE[0]-10, BOXSIZE[1]-10))

	email = pygame.image.load("Images/email_button.png")
	email = pygame.transform.scale(email, (90, 40))

	pos = POS

	bobfactor = 1
	boboffmax = 3
	boboffmin = -3
	bobcount =0

	def __init__(self, id):
		self.id = id
		self.avatar = pygame.image.load("UserInfo/Avatars/%s.png" % self.id)
		self.avatar = pygame.transform.scale(self.avatar, AVATARSIZE)
		self.usr = json.load(open("UserInfo/%s.json" % self.id, "rb"))
	
		self.label = self.myfont.render(self.usr['name'], 1, (0,0,0))
		self.name = self.myfont.render("Name: %s" % self.usr['name'], 1, (0,0,0))
		self.contact = self.myfont.render("Contact: %s" % self.usr['contact'], 1, (0,0,0))
		self.idlabel = self.myfont.render("ID: %s" % self.usr['id'], 1, (0,0,0))

		tags = self.usr['tags']
		self.tag_labels = [self.myfont.render(tag, 1, (0,0,0)) for tag in tags]
		self.tag = self.myfont.render("Working on: ", 1, (0,0,0))

		self.boboffset = randint(self.boboffmin+1, self.boboffmax-1)
		#print self.boboffset
		

	def display(self, screen):
		# display box
		if self.bobcount == 2:
			if self.boboffset >= self.boboffmax or self.boboffset <= self.boboffmin:
				self.bobfactor *= -1
			self.boboffset += self.bobfactor
			self.bobcount = 0
		else:
			self.bobcount += 1

		p = (self.pos[0], self.pos[1] + self.boboffset)

		screen.blit(self.bg1, p)
		# screen.blit(self.bg2, (self.pos[0]+ 5, self.pos[1] + 5))

		avpos = (p[0] + BOXSIZE[0]/2 - AVATARSIZE[0]/2, p[1]+AVATARSIZE[1]/2)
		# Draw avatar
		screen.blit(self.avatar, avpos)
		# Draw name
		screen.blit(self.name, (self.pos[0] + 30, self.pos[1] + avpos[1]))
		screen.blit(self.contact, (self.pos[0] + 30, self.pos[1] + avpos[1] + 30))
		screen.blit(self.idlabel, (self.pos[0] + 30, self.pos[1] + avpos[1] + 60))

		screen.blit(self.tag, (self.pos[0] + 30, self.pos[1] + avpos[1] + 110))
		cnt = 0
		y = self.pos[1] + avpos[1] + 110
		x = self.pos[0] + 30 + self.tag.get_width()
		for tag in self.tag_labels:
			print x + tag.get_width()
			if x + tag.get_width() > SCREENSIZE[0]/2 + BOXSIZE[0]/2 - 30:
				y += 30
				x = self.pos[0] + 30 + self.tag.get_width()

			screen.blit(tag, (x,y))

			x += tag.get_width() + 5
			cnt += 1


		screen.blit(self.email, (p[0] + BOXSIZE[0]/2 - self.email.get_width()/2, p[1] + BOXSIZE[1] - self.email.get_height() - 10))

		#pygame.display.update()
		
	def get_avatar(self):
		return self.avatar

	def get_name(self):
		return self.usr['name']

	def set_pos(self, pos):
		self.position = pos

	def get_pos(self):
		return self.position

	def bob(self):
		if self.bobcount == 2:
			if self.boboffset >= self.boboffmax or self.boboffset <= self.boboffmin:
				self.bobfactor *= -1
			self.position = (self.position[0], self.position[1] + self.bobfactor)
			self.boboffset += self.bobfactor
			self.bobcount = 0
		else:
			self.bobcount += 1
		

class WordleWord():
	"""
	WordleWord:	Takes in a word,looks in JSON file for matching word,
				Reads in information for display.
	"""
	pygame.font.init()
	header = pygame.font.SysFont("arial", 30)
	myfont = pygame.font.SysFont("arial", 15)
	
	#screen = pygame.display.set_mode(SCREENSIZE)
	#pygame.display.set_caption('Wordle') 

	bg1 = pygame.image.load('Images/wordlebubble.png')
	bg1 = pygame.transform.scale(bg1, BUBBLESIZE)
	# bg2 = pygame.image.load('Images/WordleWordBox2.gif')
	# bg2 = pygame.transform.scale(bg2, (BOXSIZE[0]-10, BOXSIZE[1]-10))

	pos = POS
	position = pos

	bobfactor = 1
	boboffmax = 3
	boboffmin = -3
	bobcount =0

	def __init__(self, w):
		for f in json.load(open('WordleWord.json', 'r')):
			if f['id'] == w:
				self.word = f['word']
				assoc = f['associated']	
		# Read in relevant information from JSON file
		self.associated = [Staff(ass) for ass in assoc]
		for i in xrange(len(self.associated)):
			AvatarPos = (self.pos[0] + AVATARSIZE[0], 
							self.pos[1] +70+ AVATARSIZE[1]/2 + ((AVATARSIZE[1]+30)*i))
			self.associated[i].set_pos(AvatarPos)

		self.label = self.header.render(self.word, 1, (0,0,0))
		self.boboffset = randint(self.boboffmin+1, self.boboffmax-1)

	def display(self, screen=None):
		# display box
		# print self.word
		if self.bobcount == 2:
			if self.boboffset >= self.boboffmax or self.boboffset <= self.boboffmin:
				self.bobfactor *= -1
			self.boboffset += self.bobfactor
			self.bobcount = 0
		else:
			self.bobcount += 1

		p = (self.pos[0], self.pos[1] + self.boboffset)

		screen.blit(self.bg1, p)
		# screen.blit(self.bg2, (self.pos[0]+ 5, self.pos[1] + 5))
		screen.blit(self.label, (p[0] + BUBBLESIZE[0]/2 - self.label.get_width()/2, p[1] + 25))
		# display avatars
		# print self.associated
		for ass in self.associated:
			ass.bob()
			AvatarPos = ass.get_pos()
			Avatar = ass.get_avatar()
			x_pos, y_pos = self.pos
			# Put in bubble
			screen.blit(self.bg1, (x_pos, AvatarPos[1] + Avatar.get_height()/2 - BUBBLESIZE[1]/2))
			# Put in Avatar
			screen.blit(Avatar, AvatarPos)
			# Put in name
			screen.blit(ass.label, (AvatarPos[0] + AVATARSIZE[0] + 15, AvatarPos[1] - 7 + AVATARSIZE[1]/2))

	def check_click(self, point):
		i = 0
		found = -1
		for ass in self.associated:
			p = ass.get_pos()
			# If avatar xpos < point xpos < avatar xpos + get_width
			if point[0] > p[0] and point[0] < p[0]+ AVATARSIZE[0] + ass.label.get_width() + 30:
				# If avatar ypos < point ypos < avatar ypos + height
				if point[1] > p[1] and point[1] < p[1] + AVATARSIZE[1]:
					found = i
			i += 1
		return found

	def change_state(self, state, eventpos):
		new_state = state
		av = -1
		if state == 'tank':
			new_state = "word"
		elif state == "word":
			av = self.check_click(eventpos)
			# If button
			if av >= 0:
				#Change state
				new_state = "user"
			# Else close screen
			else:
				new_state = 'tank'
		elif state == 'user':
			new_state = 'word'
		
		return (new_state, av)
	
	def main(self):
		pygame.init()

		state = "tank"
		showWord = False
		showUser = False

		while True: #Game Loop - Detects Key Presses and Mouse Clicks 
			for event in pygame.event.get():
				if event.type == QUIT:
					pygame.display.quit()
					exit()
				elif event.type == pygame.MOUSEBUTTONDOWN:
					if event.button == 1:   #Left Click
						state, av = self.change_state(state, event)

				elif event.type == pygame.MOUSEBUTTONUP:	#After mouse button has been released
					if event.button == 1:
						pass
					if event.button == 3:
						pass
			if state == "word":
				self.display()
			elif state == "user":
				self.associated[av].display(self.screen)


if __name__ == '__main__':
	s = [	
			{'id': 'word2', "word": "Hana", "associated": ["Ray", "Mark", "Eimear", "Peter"]}, 
			{'id': 'word0', "word": "Java", "associated": ["Ray", "Mark"]},
			{'id': 'word3', 'word': 'User Testing', 'associated': ["Eimear", 'Peter']},
			{'id': 'word4', 'word': 'Python', 'associated': ["Ray", 'Mark']},
			{'id': 'word1', 'word': 'SharePoint', 'associated': ["Eimear", 'Peter']}
		]
	json.dump(s, open('WordleWord.json', 'wb'))

	#print pygame.font.get_fonts()

	a = WordleWord("word2")
	a.main()
